package org.compiere.process;

import java.math.BigDecimal;

import org.compiere.apps.ADialog;
import org.compiere.model.MBSCCard;
import org.compiere.model.MBSCCardLine;
import org.compiere.model.MBSCCardLineLink;
import org.compiere.util.Env;
import org.compiere.util.Trx;

public class BSCCascadeCardLine extends SvrProcess {

	private int BSC_CardLine_Link_ID = 0;
	private MBSCCardLineLink cardLineLink = null;
	private MBSCCardLine fromCardLine;
	private MBSCCard toCard;
	private int AD_Client_ID = 0 ; // Env.getAD_Client_ID(getCtx());
	private int AD_Org_ID = 0; // Env.getAD_Org_ID(getCtx());
	private String resultIt = null;
	private String trxName = Trx.createTrxName();
	
	@Override
	protected void prepare() {
		AD_Client_ID = Env.getAD_Client_ID(getCtx());
		AD_Org_ID = Env.getAD_Org_ID(getCtx());
		initParameters();

	}

	@Override
	protected String doIt() throws Exception {
		loadCards();
		if (toCard != null && fromCardLine != null && cardLineLink != null) {
			MBSCCardLine toCardLine = toCard.cascade(fromCardLine);
			if (toCardLine != null) {
				cardLineLink.setlink_BSC_CardLine_ID(toCardLine.getBSC_CardLine_ID());
				if (cardLineLink.save())
					resultIt = "Каскадирование завершено успешно";
				else
					resultIt = "Каскадирование не удалось";
			} else {
				resultIt = "Каскадирование не удалось";
			}
		}
		return resultIt;
	}

	private void initParameters() {
		for(ProcessInfoParameter param: getParameter()) {
			if (param.getParameterName().equals("BSC_CardLine_Link_ID")) {
				BigDecimal BSC_CardLine_Link_ID = (BigDecimal) param.getParameter();
				setBSC_CardLine_Link_ID(BSC_CardLine_Link_ID.intValue());
			}
		}
	}

	public int getBSC_CardLine_Link_ID() {
		return BSC_CardLine_Link_ID;
	}

	public void setBSC_CardLine_Link_ID(int bSC_CardLine_Link_ID) {
		BSC_CardLine_Link_ID = bSC_CardLine_Link_ID;
	}

	protected void loadCards() {
		if(BSC_CardLine_Link_ID > 0) {
			cardLineLink = new MBSCCardLineLink(Env.getCtx(),BSC_CardLine_Link_ID,null);
			if (cardLineLink != null) {
				if (cardLineLink.getlink_BSC_CardLine_ID() == 0 && cardLineLink.getC_BPartner_ID() > 0) {
					fromCardLine = new MBSCCardLine(Env.getCtx(),cardLineLink.getBSC_CardLine_ID(),null);
					if (fromCardLine != null) {
						int C_BPartner_ID = cardLineLink.getC_BPartner_ID();
						int C_Period_ID = cardLineLink.getC_Period_ID();
						toCard = MBSCCard.get(C_BPartner_ID, C_Period_ID, null);
						if (toCard == null) {
							String BPartnerName = cardLineLink.getC_BPartner().getName();
							String PeriodName = cardLineLink.getC_Period().getName();
							String AD_Message = "Нет карты ССП для "+BPartnerName + " за " + PeriodName+".\n Создать?"; 
							boolean b = ADialog.ask(25, null, AD_Message);
							if (b)
								toCard = createToCard();
						}
					} else {
						resultIt = "Карта ССП для каскадирования не найдена!";
					}
				} else {
					resultIt ="Каскадирование не трубется!";
				}
			}
		}
	}
	
	protected MBSCCard createToCard() {
		MBSCCard result = null;
		if (fromCardLine != null) {
			MBSCCard fromCard = fromCardLine.getCard();
			result = new MBSCCard(Env.getCtx(),0,null);
			result.copyFrom(fromCard,true);
			result.setC_BPartner_ID(cardLineLink.getC_BPartner_ID());
			result.setC_Period_ID(cardLineLink.getC_Period_ID());
			result.save();
		}
		return result;
	}
}
